<?php
// 课程收藏
require(dirname(__FILE__) . '/base.class.php');
class Collect extends Base
{
    function __construct()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        parent::__construct();
        if(empty($this->openid)){
            $output['errcode']=500;
            $output['msg']='缺少openid';
            die(json_encode($output));
        }
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    // 获取分类列表
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql = "select * from course_collects limit {$start},{$page_num}";
        $list = $this->db->query($sql);
        if ($list) {
            // 获取课程详情和用用户详情
            foreach ($list as $key => $item) {
                $list[$key]['user']=$this->getUser($item['user_id']);
                $list[$key]['course']=$this->getCourse($item['course_id']);
            }
            $output['data'] = $list;
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取收藏列表失败';
        }
        echo json_encode($output);
    }


    // 添加收藏
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $data['course_id']=$_POST['course_id'];
            $data['openid']=$this->openid;
            $query_str = arr2str($data);
            $sql = "insert into course_collects set $query_str";
            $res = $this->db->query($sql);
            if ($res) {
                // 将收藏id返回
                $output['collect_id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }
    /*
        删除收藏
    */ 
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "delete from course_collects where id={$id}";
            $res = $this->db->query($sql);
            if (!$res) {
                $output['errcode'] = 3;
                $output['msg'] = '删除失败';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    // 获取用户信息
    private function getUser($id){
        $user=$this->db->get_one("select * from users where id={$id}");
        $user['avatar']=path($user['avatar']);
        return $user;
    }
    // 获取课程信息
    private function getCourse($id){
        $course=$this->db->get_one("select * from courses where id={$id}");
        $course['cover']=path($course['cover']);
        return $course;
    }
}
